package jwlee.testitext;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.PdfWriter;

public class XmlPdfTransformer {
	private static String path = System.getProperty("user.dir")
			+ "\\src\\jwlee\\testitext\\";

	public static void main(String[] args) throws Exception {
//		transByURL();
		transByFile();
//		tt();
	}

	private static void tt() throws UnsupportedEncodingException,
			FileNotFoundException, DocumentException {
		String filename = "weather";
		String inputFile = path + filename + ".xml";
		String outputFile = path + filename + ".pdf";
		
		String htmText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><weather><station><location>中国</location><description>Sunny</description><tempf>85</tempf></station></weather>";      
        htmText = new String(htmText.getBytes("gb2312"), "ISO8859_1");
        ByteArrayInputStream is = new ByteArrayInputStream(htmText.getBytes());
        Document doc = new Document(PageSize.A4, 80, 50, 30, 65);
        OutputStream os = new FileOutputStream(outputFile);
        PdfWriter.getInstance(doc, os); 
        doc.open();
        ITextSurportHtmlParser p = new ITextSurportHtmlParser();                
        p.goGB(doc, is);
        doc.close();
	}
	
	private static void transByURL() throws DocumentException, IOException {
		String filename;
		filename = "poem";
		filename = "book";
		filename = "weather";
		String inputFile = path + filename + ".xml";
		String url = new File(inputFile).toURI().toURL().toString();
		String outputFile = path + filename + ".pdf";
		OutputStream os = new FileOutputStream(outputFile);
		ITextRenderer render = new ITextRenderer();
		render.setDocument(url);
		render.layout();
		render.createPDF(os);

		os.close();
		System.out.println("generate file: " + outputFile);
	}

	private static void transByFile(){
		String filename;
		filename = "book";
		filename = "poem";
		filename= "weather";
		String inputFile = path + filename + ".xml";
		String outputFile = path + filename + ".pdf";

		try {
			OutputStream os = new FileOutputStream(outputFile);
			ITextRenderer renderer = new ITextRenderer();

			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			org.w3c.dom.Document doc = builder.parse(new FileInputStream(
					new File(inputFile)));
			renderer.setDocument(doc, null);
			// renderer.setDocument(new File(inputFile));
			// renderer.setDocument(inputFile);//Can't load the XML resource

			renderer.layout();
			renderer.createPDF(os);
			os.close();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		System.out.println("generate file: " + outputFile);
	}
}
